﻿using CSharpMesInterfaceDLL;
using CSharpStationOperate;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using static CSharpMesInterfaceDLL.Mes;
using static System.Windows.Forms.LinkLabel;

namespace Test
{
    internal static class Program
    {
        static Mutex _appMutex;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // 1. 创建全局Mutex确保单实例 
            _appMutex = new Mutex(true, @"Global\MesApp", out var _isFirstInstance);
            //2.非首次启动时处理参数传递
            if (!_isFirstInstance)
            {
                //MessageBox.Show("mes app 已启动");
                //if (args.Length == 0)
                //{
                //    args = new string[] { "Login" };
                //}
                //SingalNativeMathods.SendArgsToRunningInstance(args, "MES接口服务");
                //Thread.Sleep(50);
                return;
            }            

            //string para = string.Empty;
            //if (args.Length > 0 )
            //{
            //    para = args[0];
            //}
            //if (string.IsNullOrEmpty(para))
            //{
                ////调用登陆
                //if (Station.Instance.LoginMes(out _))
                //{
                //    //登陆成功
                //    //Station.Instance.DownloadME_PEConfig(Station.Instance.GetStationInfo(), out var msg);     // 无需调用该函数
                //    //站点：Oper
                //    string oper = Station.Instance.GetStationInfo().oper;
                //    //拉线
                //    string line = Station.Instance.GetStationInfo().line;
                //    //登录机型
                //    string model = Station.Instance.GetMfgPlanInfo().model;
                //    string path = Station.Instance.VisionConfigPath;
                //    Console.WriteLine($"oper={oper},lineId={line},productModel={model},configPath={path}");
                //    //MessageBox.Show($"配置文件保存路径:({path})");
                //}
                //else
                //{
                //    string test = @"2025-07-03 15:04:19.6884|INFO|CSharpMesInterfaceDLL.NetHttp|request[
                //    server: AateAsyApiGetToolFtpInfo.json
                //    postdata: {
                //      ""apServer"": ""asy1.myoas.com""
                //    }
                //    ]
                //    2025-07-03 15:04:19.7158|INFO|CSharpMesInterfaceDLL.NetHttp|response[
                //    server: AateAsyApiGetToolFtpInfo.json
                //    outdata:
                //    {""success"":true,""statusvalue"":""0"",""msgcate"":""S"",""msgcode"":""CMN-0000"",""msg"":""CMN-0000: 操作已成功。"",""ftpServer"":""teftp-dg.oppoit.com"",""ftpPort"":""21"",""ftpUserName"":""te-rwd"",""ftpPassWord"":""n*d73uj$ksmD8a"",""ftpPassWordEncrypt"":""bipkNzN1aiRrc21EOGE=""}
                //    ]


                //    oper=K5181,lineId=PKB10827,productModel=PKB110CN,configPath=D://OVisionPlusConfig/VisionConfig/PKB110CN/星野黑/16GB+512GB
                //    `";
                //    Console.WriteLine(test);
                //}
            //}

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            GC.KeepAlive(_appMutex); // 保持Mutex激活
        }
    }
}
